import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_architecture/constant/route_constants.dart';
import 'package:flutter_architecture/routes/routes.dart';
import 'package:flutter_architecture/utils/shared_preferences_util.dart';
import 'package:flutter_architecture/views/common/welcome_page.dart';
import 'package:flutter_architecture/widgets/protocol/protocol_widget.dart';
import 'package:logger/logger.dart';
import 'package:flutter_architecture/utils/log_util.dart';
import 'package:fluro/fluro.dart';
/// App index page
/// todo 权限申请
/// @author: dingwen
/// @date: 2020/12/28

class IndexPage extends StatefulWidget {

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> with ProtocolWidget{

  @override
  void initState() {
    super.initState();
    //todo 获取设备信息，绑定等
    //todo 动态权限申请
    //用户协议
    Future.delayed(Duration.zero,()=> userProtocolGo());
  }

  ///热启动欢迎页面
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Image.asset('images/flutter_app_ding2.png'),
      ),
    );
  }

  ///关闭当前App，当用户没有同意用户协议时
  void closeApp()=> SystemChannels.platform.invokeMethod("SystemNavigator.pop");

  void userProtocolGo()async {
    await SPUtil.init();
    bool isAgreement = await SPUtil.getBool('isAgreement');

    if(isAgreement == null || !isAgreement){
      isAgreement = await showUserProtocol(context);
      SPUtil.save('isAgreement', isAgreement);
    }
    if(isAgreement){
      //进入
      next();
      log(level: Level.info,fileName: 'index_page.dart',functionName: 'userProtocolGo',message: '用户已经同意用户协议');
    }else{
      log(level: Level.info,fileName: 'index_page.dart',functionName: 'userProtocolGo',message: '用户不同意用户协议，已关闭App');
      closeApp();
    }
  }

  void next() async{
    //是否为第一次进入App
    bool isFirst =  await SPUtil.getBool('flutter_architecture_isFirst');
    if(isFirst == null || !isFirst){
      //splash
      Routes.pageTo(context, splash_page);
//      Routes.push(context, settings: RouteSettings(name: splash_page));
    }else{
      //welcome
      Routes.push(context, settings: RouteSettings(name: welcome_page));
    }
  }
}
